﻿using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Mvc;
using MachineVision.API.SignalR;
using MachineVision.API.Models;
using Microsoft.AspNetCore.Authorization;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MachineVision.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    //[Authorize]//(Roles = "Admin")
    public class CtrlController : ControllerBase
    {
        private readonly IHubContext<SignalRHub> _hubContext;

        public CtrlController(IHubContext<SignalRHub> hubContext)
        {
            this._hubContext = hubContext;
        }
        //// GET: api/<Ctrl>
        //[HttpGet]
        //public IEnumerable<string> Get()
        //{
        //    return new string[] { "value1", "value2" };
        //}

        // GET api/<Ctrl>/5
        [HttpGet("{id}")]
        public async Task<RobotState> Get(string id)
        {
            var cid = _hubContext.Clients.User;

            return new RobotState();
        }




        // POST api/<Ctrl>
        [HttpPost]
        public async Task<string> Post([FromBody]string id, string value)
        {
            try
            {
                //TODO
                var cid = _hubContext.Clients.User(id);
                await _hubContext.Clients.Client(id).SendAsync("ShowMsg", value);
                return "OK";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        //// PUT api/<Ctrl>/5
        //[HttpPut("{id}")]
        //public void Put(int id, [FromBody] string value)
        //{
        //}

    }
}
